program examp
var x,y,r,min,n,m,a:integer;
begin
 min:=369;
 a:=(369-39)div 69;x:=1;
 while(x<=a)do
  begin
   y:=(369-69*x)div 39;
   r:=369-69*x-39*y;
   if(r<min)then
   begin
    min:=r;n:=x;m:=y;
    break
   end
   else 
    continue;
   x:=x+1
  end;
 write('min=',min,'x=',n,'y=',m)
end